package com.songhq.data.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.songhq.data.bean.PageVO;
import com.songhq.data.bean.Response;
import com.songhq.data.bean.User;
import com.songhq.data.constant.ServiceCode;
import com.songhq.data.log.util.LogUtil;
import com.songhq.data.service.UserService;

@RequestMapping("user")
@RestController
public class UserController {

	
	@Autowired
	private UserService userService;
	
	@RequestMapping("login")
	public Response login(@RequestParam("name") String name,@RequestParam("password")String password) {
		try {
    		LogUtil.info("UserController.login|name="+ name +",password=" + password);
            return userService.login(name,password);
        } catch (Exception e) {
        	LogUtil.error(e, "FigureController.listFigures");
            return Response.error(ServiceCode.FAIL, "用户登入异常");
        }
	}

	@RequestMapping("loginout")
	public Response loginout(HttpServletRequest request) {
		try {
			String userId = request.getHeader("userId");
			LogUtil.info("UserController.loginout|userId="+userId);
			userService.loginout(userId);
            return Response.ok();
        } catch (Exception e) {
        	LogUtil.error(e, "FigureController.loginout");
            return Response.error(ServiceCode.FAIL, "用户注销异常");
        }
	}
	@RequestMapping(value = "signin" ,method = RequestMethod.POST)
	public Response signin(User user,@RequestParam("confirm") String confirm) {
		try {
			LogUtil.info("UserController.signin|user="+JSON.toJSONString(user) + ",confirm=" + confirm );
            return userService.signin(user,confirm);
        } catch (Exception e) {
        	LogUtil.error(e, "FigureController.signin");
            return Response.error(ServiceCode.FAIL, "用户注册异常");
        }
	}
}
